<?php
// +----------------------------------------------------------------------
// | 壁葬销售流程
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use tree\Tree;

class VisualController extends AdminBaseController
{

    public function initialize()
    {
        $adminSettings = cmf_get_option('admin_settings');
        if (empty($adminSettings['admin_password']) || $this->request->path() == $adminSettings['admin_password']) {
            $adminId = cmf_get_current_admin_id();
            if (empty($adminId)) {
                session("__LOGIN_BY_CMF_ADMIN_PW__", 1);//设置后台登录加密码
            }
        }

        parent::initialize();
    }
    public function index(){





        return $this->fetch();
    }
    public function getSup($id){
        // 拼接查询条件hql
        $res=Db::name("cemetery")->where(array('is_delete'=>0,'id'=>$id))
            ->select()->toArray();
        // 得到一级数据
        if($res) {
            if($res[0]["parent_id"] != 0) {
                $result = $this->getSup($res[0]["parent_id"]);
            }else{
                $result = array();
            }
            $imgInfo = array_merge_recursive($res, $result);
        }else{
            $imgInfo = array();
        }
        return $imgInfo;
    }
	public function register(){
		$this->cemetery_model = Db::name("cemetery");
        $name = $this->request->param("name");
        //import("Tree");
        $menu = new Tree();
        $menu->icon = array('&nbsp;', '&nbsp;', '&nbsp;');
        $menu->nbsp = '';
        $result =  $this->cemetery_model->where(array('is_delete'=>0)) ->where('name','like','%'.$name.'%')->order(array("list_order" => "ASC"))->select()->toArray();;
        if(!empty($name)){
            for($i=0;$i<count($result);$i++){
                $arr1 = $this->getSup($result[$i]["parent_id"]);
                $imgInfo = array_merge_recursive ( $arr1, $result );
            }
            $key = "id";//去重条件
            $tmp_arr = array();//声明数组
            foreach($imgInfo as $k => $v) {
                if(in_array($v[$key], $tmp_arr)){ //搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
                    unset($imgInfo[$k]);//删除掉数组（$arr）里相同ID的数组
                } else {
                    $tmp_arr[] = $v[$key];//记录已有的id
                }
            }
            $result = $imgInfo;
        }
		
        $newmenus=array();
        foreach ($result as $m){
        	$newmenus[$m['id']]=$m;
        }
		//dump($result);exit;
        foreach ($result as $n => $t) {
        	$result[$n]['level'] = $this->_get_level($t['id'], $newmenus);
        	$result[$n]['parentid_node'] = ($t['parent_id']) ? ' class="child-of-node-' . $t['parent_id'] . '"' : '';
			
        }
        $tom = array();
        foreach($result as $k => $v){
            $tombarea = Db::name("tombarea")->field("id,name,'' as type,1 as status,'' as remark,'' as cover_path,'' as list_order,3 as is_generate,0 as is_delete")
                ->where(array("cemetery_id"=>$v["id"],"status"=>1))
                ->select()->toArray();
            foreach($tombarea as $i => $t){
                $tombarea[$i]['parent_id'] = $v["id"];
                $tombarea[$i]['level'] = $v["level"]+1;
                $tombarea[$i]['parentid_node'] = ' class="child-of-node-' . $v["id"]. '"';
            }
            $tom = array_merge_recursive ( $tom, $tombarea);
        }
        $result = array_merge_recursive ( $tom, $result);
        $str = "<tr id='node-\$id' \$parentid_node>
				   <td style='padding-left:30px; cursor:pointer'><span name='title' id='\$id' data-parent='\$is_generate' \$parentid_node>\$spacer \$name</span></td>
				</tr>";
        $menu->init($result);
        $categorys = $menu->getTree(0, $str);
        $this->assign("categorys", $categorys);
        $this->assign("name", $name);
		return $this->fetch('index');
	}
	/*寄存*/
	public function sale(){
		return $this->fetch();
	}
	/*申请人*/
	public function applyer(){
		return $this->fetch();
	}
	/*使用人*/
	public function user(){
		return $this->fetch();
	}
	/*订单*/
	public function Orderpay(){
		return $this->fetch();
	}
	/*合同*/
	public function signcontract(){
		return $this->fetch();
	}
	/*商品选购*/
	public function choosegoods(){

        //2为商品类型
        $where = array(
            'd.type'		=> 2,
            'd.is_delete'	=> 0,
            'parent_id'		=> 0
        );
        //取库房数据
        $results = $this->deal_model->alias('d')->where($where)->order('sort_order ASC,d.id DESC')->paginate(12);
        $this->assign("results",$results);






		return $this->fetch();
	}
		/**
     * 获取菜单深度
     * @param $id
     * @param $array
     * @param $i
     */
    protected function _get_level($id, $array = array(), $i = 0) {
        
        	if ($array[$id]['parent_id']==0 || empty($array[$array[$id]['parent_id']]) || $array[$id]['parent_id']==$id){
        		return  $i;
        	}else{
        		$i++;
        		return $this->_get_level($array[$id]['parent_id'],$array,$i);
        	}
        		
    }
}
